import { BaseGraph, IBaseProps, IBoundBox } from './base'

export class Group extends BaseGraph {
  public children: Array<BaseGraph> = []
  constructor(props?: IBaseProps) {
    super(props)
  }
  public add(graph: BaseGraph) {
    if (graph.type == 'text' || graph.type == 'group') {
      graph.ctx = this.ctx
      graph.setPath()
    }
    graph.setOption(this.option)
    this.children.push(graph)
  }
  public draw(ctx: CanvasRenderingContext2D): void {
    for (let i = 0; i < this.children.length; i++) {
      this.children[i].draw(ctx)
    }
  }
  public clone() {
    let group = new Group({ ...this.option, ...this.getBaseCloneAttr() })
    group.children = this.children.map((item) => item.clone())
    return group
  }
  public getBoundBox(): IBoundBox {
    let top = Infinity
    let left = Infinity
    let bottom = -Infinity
    let right = -Infinity
    for (let i = 0; i < this.children.length; i++) {
      let box = this.children[i].getBoundBox()
      top = top < box.top ? top : box.top
      left = left < box.left ? left : box.left
      bottom = bottom > box.bottom ? bottom : box.bottom
      right = right > box.right ? right : box.right
    }
    return { top, left, bottom, right, width: right - left, height: bottom - top }
  }
  public setPath() {}
  public drawPath(ctx: CanvasRenderingContext2D): void {}
}
